package cn.edu.scau.cmi.maintain.controller;

import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Picture;
import cn.edu.scau.cmi.ema.service.PictureServiceImpl;
import cn.edu.scau.cmi.front.controller.FrontCmiEmaController;
import cn.edu.scau.cmi.maintain.service.PictureMaintenceService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import java.awt.*;
import java.io.File;
import java.util.Iterator;
import java.util.Set;

/**
 * 原来是在测试源文件夹中的方法，简化起见，放到
 * 
 */

@Controller()

//@PropertySource(value = { "classpath:food2type.properties" })

public class MaintainPictureController extends FrontCmiEmaController {
	
	@Autowired
	PictureMaintenceService testPictureService;

	
	@RequestMapping(value = "/updatePicture", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public void updatePicture() throws Exception {
		Set<Picture> pictures = pictureDAO.findAllPictures();
		Iterator<Picture> picturesIterator = pictures.iterator();
		PictureServiceImpl pictureServiceImpl = new PictureServiceImpl();
		while (picturesIterator.hasNext()) {
			Picture picture = picturesIterator.next();
			picture.setName("event_"+picture.getEvent().getId()+"_0");
			picture.setPath("event_"+picture.getEvent().getId()+"_0");
			pictureServiceImpl.savePicture(picture);
			System.out.println(picture.getName() + "已经修改！！！");
		}
	}
	
	
	
	
	@RequestMapping(value = "/updatePicturePathAndFilename", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public void updatePicturePathAndFilename() throws Exception {
//		！！！！！！使用的时候请注意，因为使用了懒加载，因此后面会有代理错误，将懒加载去掉后测试。
		Set<Picture> pictures = pictureDAO.findAllPictures();
		Iterator<Picture> picturesIterator = pictures.iterator();
		while (picturesIterator.hasNext()) {
			Picture picture = picturesIterator.next();
			testPictureService.updatePathAndFilename(picture);
			System.out.println(picture.getName() + "已经修改！！！");
		}
	}
	
	
//	2020年5月28日测试，根据图片更新数据库，从数据库的picture表中获取文件名，如果不存在这个文件，删除这条记录，
//	根据已经审核的图片更新数据库
	@RequestMapping(value = "/deletePictureRecordAccrodingPictureFile", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public void deletePictureRecordAccrodingPictureFile() throws Exception {
		Set<Picture> pictures = pictureDAO.findAllPictures();
		Iterator<Picture> picturesIterator = pictures.iterator();
		while (picturesIterator.hasNext()) {
			Picture picture = picturesIterator.next();
			File picutreFile=new File("D:\\EMA工作目录\\Image\\已经审核\\"+picture.getName());
//			System.out.print("图片文件名是："+picture.getName());
			if(!picutreFile.exists()) {
				System.out.print("不存在，");
				pictureService.deletePicture(picture);
				System.out.println("该图片的在数据库表Picture中的记录已经删除！！！！！！");
			}else {
				System.out.println("图片文件名："+picture.getName()+"存在！！！！！！");
			}
				
		}
	}
	
//	有的有图片，但是picture表中没有记录，应该根据表名，添加上这些记录。
	@RequestMapping(value = "/addPictureRecordAccrodingPictureFile", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public void addPictureRecordAccrodingPictureFile() throws Exception {
//		FileUtils.iterateFiles(new File("d:/EMA工作目录/Image/已经审核"), "jpg", 0);
		
	}
	
	
//	创建几个测试图片用于测试
	@RequestMapping(value = "/createImage4Event", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public void createImage4Event() throws Exception {
		String destPath="d:/EMA工作目录/Image/已经审核";
		Set<Event> events = eventDAO.findAllEvents();
		Iterator<Event> eventsIterator = events.iterator();
		while (eventsIterator.hasNext()) {
			Event event = eventsIterator.next();
//			System.out.println(picture.getName());
//			每个事件都生成3个图片
//			修改为随机生成图片0到5个，方便测试
			int pictureQuantity=(int) (Math.random()*8.0);
			
			
			for(int i=0;i<pictureQuantity;i++) {
				Picture picture=new Picture();
				picture.setEvent(event);
				String pictureName="event_"+event.getId()+"_"+i+".jpg";
				picture.setName(pictureName);
				Color backgroudnColor=new Color(0);
				Color paintColor=new Color(0);

				word2ImageService.getImage(event, new Font("仿宋",Font.PLAIN,80), 1000, 600, destPath, pictureName,backgroudnColor, paintColor);
//				这个功能时用于给事件的图片，不是食物，因此设置为空
//				picture.setFood(event.getFoodbatch().getFood());
				Food food=foodDAO.findFoodById(-2);
				picture.setFood(food);
				picture.setPath(picture.getName());
				pictureService.savePicture(picture);
			}
			System.out.println(event.getName()+"的"+pictureQuantity+"个图片已经生成！！！");
		}
	}
	

//	创建几个测试图片用于测试
//	@Test
	@SuppressWarnings("unused")
	public void createIcon4Project() throws Exception {
		String destPath="d:/EMA工作目录/Image/项目图标";

		JSONObject entitiesJsonObject = new JSONObject();
		
		Metamodel metamodel = entityManager.getMetamodel();
		Set<EntityType<?>> entities = metamodel.getEntities();
		for(EntityType<?> entity : entities) {
			entity.getName();
			Picture picture=new Picture();
			String pictureName=entity.getName().toLowerCase() + ".png";
			picture.setName(pictureName);
			Color paintColor=new Color(0);

			
			Color backgroundColor =new Color(0);;
			word2ImageService.getImage(entity.getName(), new Font("仿宋", Font.BOLD, 50), 300, 200, destPath, pictureName, backgroundColor, paintColor);
		}
	}
}